﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FarmerStore.Models;
using System;
using FarmerStore.ViewModels;

namespace FarmerStore.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        FarmerStoreEntities storeDB = new FarmerStoreEntities();

        public ActionResult Index()
        {
            // Get most popular albums
            var albums = GetTopSellingProducts(5);

            return View(albums);
        }

        public ActionResult Advert()
        {
            // Get most popular albums
            var albums = this.GetSpecialAdverts(2);

            return View(albums);
        }

        public ActionResult MainAdverts()
        {
            // Get most popular albums
            var albums = this.GetSpecialAdverts(1);

            return View(albums);
        }

        private List<Advert> GetSpecialAdverts(int typeId)
        {
            // Group the order details by product and return
            // the albums with the highest count
            DateTime nowDateTime=DateTime.Now;
            return storeDB.Adverts.Where(item=>item.StartDate<=nowDateTime && nowDateTime<=item.EndDate && item.TypeId==typeId)
                .ToList();
        }

        public ActionResult HotDeals()
        {
            // Retrieve list of Genres from database
            var hotDeals = storeDB.GetHotDeals(0);

            // Return the view
            return View(hotDeals);
        }

        private List<Product> GetTopSellingProducts(int count)
        {
            // Group the order details by product and return
            // the albums with the highest count

            return storeDB.Products
                .OrderByDescending(a => a.OrderDetails.Count())
                .Take(count)
                .ToList();
        }
    }
}
